package jp.sourceforge.akjrcp.videotools.dvdtoxvid;



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.ffmpeg.FFMpegFLVParser;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegFileInfomation;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUI;
import jp.sourceforge.akjrcp.flvtools.explorer.ThumbnailManager;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;

public class DVDTitleSetComposite extends Composite{
	
private List labels=new ArrayList();
private Button checkbox;
private DVDTitleSet titleSet;
private DVDFileList fileList;
public boolean getSlected(){
	return checkbox.getSelection();
}
public DVDTitleSetComposite(Composite parent, int style,DVDTitleSet titleSet,CheckToRadio checkToRadio) {
		super(parent, style);
		this.titleSet=titleSet;
		// TODO Auto-generated constructor stub
		setLayout(new GridLayout(4,false));
		checkbox = new Button(this,SWT.CHECK);
		checkbox.addSelectionListener(checkToRadio);//check to radio
		checkToRadio.addButton(checkbox);
		
		GridData gd=new GridData();
		gd.grabExcessVerticalSpace=true;
		gd.minimumHeight=100;
		checkbox.setLayoutData(gd);
		File files[]=titleSet.getFiles();
		fileList=new DVDFileList(this,SWT.V_SCROLL|SWT.BORDER,files,120);
		
		Composite panel=new Composite(this,SWT.NONE);
		
		
		panel.setLayout(new GridLayout(1,false));
		
		String ffmpegPath=FFMpegUI.getFFmpegOrPureFFMpeg();
		
		File file=fileList.getParseFile();
		FFMpegFLVParser parser=new FFMpegFLVParser(ffmpegPath);
		FFMpegFileInfomation info=parser.parseFile(file);
		
		CCombo audio=new CCombo(panel,SWT.READ_ONLY);
		GridData gd2=new GridData();
		gd2.minimumWidth=100;
		gd2.grabExcessHorizontalSpace=true;
		audio.setLayoutData(gd2);
		
		int asize=info.countAudioTracks();
		//log.info("asize:"+asize);
		for (int i = 0; i < asize; i++) {
			audio.add(info.getAudioCodec(i));
		}
		if(audio.getItemCount()>0){
			audio.select(0);
		}
		//audio.add("AC3");//dummy
		
		Label timeLabel=new Label(panel,SWT.NONE);
//		timeLabel.setText("00:00:23.33");//dummy 
		
		
		ScrolledComposite scroll=new ScrolledComposite(this,SWT.H_SCROLL|SWT.BORDER);
		scroll.setLayoutData(new GridData(GridData.FILL_BOTH));
		//scroll.setLayout(new RowLayout());
		
		Composite labelList = new Composite(scroll,SWT.NULL);
		//labelList.setLayoutData(new GridData(GridData.FILL_BOTH));
		labelList.setLayout(new GridLayout(9,false));
		
		
		scroll.setContent(labelList);
		scroll.setMinSize(SWT.DEFAULT,100);//default
		
		scroll.setExpandHorizontal(true);
		scroll.setExpandVertical(true);
		
		
		//make thumbs
		for (int i = 0; i < files.length&&i<9; i++) {
			File tmp;
			try {
				tmp = File.createTempFile("tmpdvd", ".jpg");
				tmp.deleteOnExit();
				if(files[i].length()<1024*1024*20){
				ThumbnailManager.makeThumbnail(files[i], 0, tmp);
				}else{
				ThumbnailManager.makeThumbnail(files[i], 3, tmp);	
				}
				Label label=new Label(labelList,SWT.NONE);
				label.setImage(new Image(null,tmp.getAbsolutePath()));//maybe problem.
				labels.add(label);
				label.setEnabled(fileList.getButtonSelection(i));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
		//scrolling
		scroll.setMinWidth(168*labels.size());
		scroll.pack();
		scroll.getParent().layout(true);
	}







public class DVDFileList extends ScrolledComposite{
private File[] files;
private Button[] buttons;
private int listHeight;
	public boolean getButtonSelection(int index){
		return buttons[index].getSelection();
	}
	public DVDFileList(Composite parent, int style,File[] files,int height) {
		super(parent, style);
		this.listHeight=height;
		this.setLayout(new FillLayout());
		Composite compo=new Composite(this,SWT.NONE);
		
		compo.setLayout(new GridLayout(1,false));
		this.files=files;
		buttons=new Button[files.length];
		DVDToXvidView.log.info("files:"+files.length);
		
		ChainSelect chainSelect=new ChainSelect();
		for (int i = 0; i < files.length; i++) {
			buttons[i]=new Button(compo,SWT.CHECK);
			
			buttons[i].setText(files[i].getName().substring(0,files[i].getName().length()-4));
			buttons[i].addSelectionListener(chainSelect);
			
			if(!files[i].getName().toUpperCase().endsWith("0.VOB")){//don't select 0 menu file
					buttons[i].setSelection(true);//default select
			}
		}
		
		
		this.setContent(compo);
		this.setMinSize(100,22*files.length);//import
		
		this.setExpandHorizontal(true);
		this.setExpandVertical(true);
		this.pack();
	}
	
	public class ChainSelect implements SelectionListener{
		
		public void widgetDefaultSelected(SelectionEvent e) {
			
		}

		public void widgetSelected(SelectionEvent e) {
			int index=0;
			for (int i = 0; i < buttons.length; i++) {
				if(e.getSource()==buttons[i]){
					index=i;
					break;
				}
			}
			if(index>0){
				boolean selection=buttons[index].getSelection();
				if(selection){
				for(int j=1;j<index;j++){
					if(buttons[j].getSelection()!=selection){
						buttons[j].setSelection(selection);
					}
					}
				}else{
					for(int j=index+1;j<buttons.length;j++){
						if(buttons[j].getSelection()!=selection){
							buttons[j].setSelection(selection);
						}
						}
				}
			}
			updateCheck();
		}
		
	}
	
	public void updateCheck(){
		for(int i=0;i<buttons.length;i++){
			if(i<labels.size()){
				((Label)labels.get(i)).setEnabled(buttons[i].getSelection());
			}
		}
	}
	
	public File getParseFile(){
//		skip index 0(menu) file,as possible
		if(files.length==1){
			return files[0];
		}else{
			if(files[0].getName().toUpperCase().endsWith("0.VOB")){
				
				return files[1];
			}else{
				return files[0];
			}
		}
	}
	
	 public Point computeSize(int wHint, int hHint, boolean changed) {

		    Point p=super.computeSize(wHint, hHint, changed);
		    p.y=listHeight;
		    return p;

		  }

	 public File[] getSelectedFiles(){
			List array=new ArrayList();
			for (int i = 0; i < buttons.length; i++) {
				if(buttons[i].getSelection()){
					array.add(files[i]);
				}
				
			}
			return (File[])array.toArray(new File[array.size()]);
		}

	
}

public File[] getEncodingFile(){
	return fileList.getSelectedFiles();
}

private long getTotalSize(){
	return 0;
}

public int getAudioTrack(){
	return 0;
}

}
